package com.hss.recyclerviewtest.itemtouch;

import android.graphics.Canvas;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.helper.ItemTouchHelper;
import android.view.ViewGroup;

//https://blog.csdn.net/a553181867/article/details/54799391
public class SimpleItemTouchHelperCallback  extends ItemTouchHelper.Callback{

    ItemTouchHelperAdapter adapter;

    public SimpleItemTouchHelperCallback(ItemTouchHelperAdapter adapter) {
        this.adapter = adapter;
    }

    @Override
    public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
        int dragFlags = ItemTouchHelper.UP | ItemTouchHelper.DOWN;
        int swipeFlags = ItemTouchHelper.LEFT;

        return makeMovementFlags(dragFlags,swipeFlags);
    }

    @Override
    public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
        int fromPostion = viewHolder.getAdapterPosition();
        int toPostion = target.getAdapterPosition();
        if (fromPostion ==0 || toPostion ==0){// 固定第一个位置
            return false;
        }
        adapter.onItemMove(viewHolder.getAdapterPosition(), target.getAdapterPosition());
        return  true;
    }

    @Override
    public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
        adapter.onItemDissmiss(viewHolder.getAdapterPosition());
    }

    //该方法返回true时，表示支持长按拖动，即长按ItemView后才可以拖动，
    // 我们遇到的场景一般也是这样的。默认是返回true
    @Override
    public boolean isLongPressDragEnabled() {
        return super.isLongPressDragEnabled();
    }

    //该方法返回true时，表示如果用户触摸并左右滑动了View，那么可以执行滑动删除操作，
    // 即可以调用到onSwiped()方法。默认是返回true。
    @Override
    public boolean isItemViewSwipeEnabled() {
        return super.isItemViewSwipeEnabled();
    }

    //我们可以在这个方法内实现我们自定义的交互规则或者自定义的动画效果。
    @Override
    public void onChildDraw(Canvas c, RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState, boolean isCurrentlyActive) {
        super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive);

        //仅对侧滑状态下的效果做出改变
  /*      if (actionState ==ItemTouchHelper.ACTION_STATE_SWIPE){
            //如果dX小于等于删除方块的宽度，那么我们把该方块滑出来
             if (Math.abs(dX) <= getSlideLimitation(viewHolder)){
                 viewHolder.itemView.scrollTo(-(int) dX,0);
             } //如果dX还未达到能删除的距离，此时慢慢增加“眼睛”的大小，增加的最大值为ICON_MAX_SIZE
             else if (Math.abs(dX) <= recyclerView.getWidth() / 2) {
                 double distance = (recyclerView.getWidth() / 2 - getSlideLimitation(viewHolder));
                 double factor = ICON_MAX_SIZE / distance;
                 double diff = (Math.abs(dX) - getSlideLimitation(viewHolder)) * factor;
                 if (diff >= ICON_MAX_SIZE) diff = ICON_MAX_SIZE;
                 ((MyAdapter.NormalItem) viewHolder).tv.setText(""); //把文字去掉
                 ((MyAdapter.NormalItem) viewHolder).iv.setVisibility(View.VISIBLE); //显示眼睛
                 FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) ((MyAdapter.NormalItem) viewHolder).iv.getLayoutParams();
                 params.width = (int) (fixWidth + diff);
                 params.height = (int) (fixWidth + diff);
                 ((MyAdapter.NormalItem) viewHolder).iv.setLayoutParams(params);
             }
        }else {
            super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive);

        }*/
    }

    //在删除了Item或者没删除而滑回原来的位置后，我们要把所做的改变重置一下，否则，
    // 会由于RecyclerView的复用而导致其他位置的ViewHolder与当前的ViewHolder所做的改变一样，
    // 即造成显示的错误。我们可以在clearView()方法内重置改变，这样就能解决因复用而导致的显示问题了。

    @Override
    public void clearView(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
        super.clearView(recyclerView, viewHolder);
        //重置改变，防止由于复用而导致的显示问题
     /*   viewHolder.itemView.setScrollX(0);
        ((MyAdapter.NormalItem)viewHolder).tv.setText("左滑删除");
        FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) ((MyAdapter.NormalItem) viewHolder).iv.getLayoutParams();
        params.width = 150;
        params.height = 150;
        ((MyAdapter.NormalItem) viewHolder).iv.setLayoutParams(params);
        ((MyAdapter.NormalItem) viewHolder).iv.setVisibility(View.INVISIBLE);*/

    }

    /**
     * 获取删除方块的宽度
     */
    public int getSlideLimitation(RecyclerView.ViewHolder viewHolder){
         ViewGroup viewGroup = (ViewGroup) viewHolder.itemView;
         return viewGroup.getChildAt(1).getLayoutParams().width;
     }

}
